//
//  LCTaskPageViewController.m
//  lechuang
//
//  Created by Mr_hi on 2023/3/31.
//  Copyright © 2023 Mooyi. All rights reserved.
//

#import "LCTaskPageViewController.h"
#import "LCTaskPageTitleCell.h"
#import "XLPageViewController.h"

#import "LCAreaTaskViewController.h"
#import "LCSearchTaskViewController.h"
#import "LCGameTaskViewController.h"
#import "LCCardPageViewController.h"

#import "LCTaskService.h"


@interface LCTaskPageViewController ()<XLPageViewControllerDelegate,XLPageViewControllerDataSrouce>

@property (nonatomic, strong) XLPageViewControllerConfig *config;
@property (nonatomic, strong) XLPageViewController *pageViewController;
@property (nonatomic, strong) NSArray *titles;

@property (strong, nonatomic)LCAreaTaskViewController * areaVC;

@property (strong, nonatomic)LCUnionModel *areaUnion;

@property (strong, nonatomic)LCSearchTaskViewController * searchVC;
@property (strong, nonatomic)LCGameTaskViewController * gameVC;
@property (strong, nonatomic)LCUnionModel *gameUnion;

@property (strong, nonatomic)LCCardPageViewController * cardVC;

@end

@implementation LCTaskPageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setEdgesForExtendedLayout:UIRectEdgeNone];
    [LCNotiCenter addObserver:self selector:@selector(taskControllerSetIndexNoti:) name:LCTaskControllerSetIndexNoti object:nil];
    self.titles = @[@"优推优选",@"网推专区",@"游戏专区",@"卡渠专区"];
    [self inistalConfig];
    [self SetMainUI];
}

- (void)inistalConfig {
    self.config = [XLPageViewControllerConfig defaultConfig];
    self.config.titleViewAlignment = 1;
    self.config.titleViewHeight = 40;
    self.config.titleWidth = (ScreenW-64)/(self.titles.count);
    self.config.titleSpace = 10;
    self.config.shadowLineColor = [UIColor clearColor];
    self.config.shadowLineHeight = 0;
    self.config.shadowLineWidth = 0;
    self.config.separatorLineColor = [UIColor clearColor];
    self.config.segmentedTintColor = [UIColor clearColor];
}

- (void)SetMainUI {
    self.pageViewController = [[XLPageViewController alloc] initWithConfig:self.config];
    self.pageViewController.view.frame = self.view.bounds;
    self.pageViewController.delegate = self;
    self.pageViewController.dataSource = self;
    self.pageViewController.selectedIndex = 0;
    self.pageViewController.bounces = NO;
    [self.view addSubview:self.pageViewController.view];
    self.pageViewController.view.backgroundColor = [UIColor colorWithHexString:@"#F2F2F2"];
    
    [self.pageViewController registerClass:LCTaskPageTitleCell.class forTitleViewCellWithReuseIdentifier:@"LCTaskPageTitleCell"];
}

-(LCAreaTaskViewController *)areaVC {
    if (!_areaVC) {
        _areaVC = [LCAreaTaskViewController xx_instantiateFromStoryboardNamed:@"Task"];
        _areaVC.parentVC = self.parentVC;
    }
    return _areaVC;
}

-(LCSearchTaskViewController *)searchVC {
    if (!_searchVC) {
        _searchVC = [LCSearchTaskViewController xx_instantiateFromStoryboardNamed:@"Task"];
        _searchVC.parentVC = self.parentVC;
    }
    return _searchVC;
}

-(LCGameTaskViewController *)gameVC {
    if (!_gameVC) {
        _gameVC = [LCGameTaskViewController xx_instantiateFromStoryboardNamed:@"Task"];
        _gameVC.parentVC = self.parentVC;
    }
    return _gameVC;
}

- (LCCardPageViewController *)cardVC {
    if (!_cardVC) {
        _cardVC = [LCCardPageViewController xx_instantiateFromStoryboardNamed:@"Task"];
        _cardVC.parentVC = self.parentVC;
    }
    return _cardVC;
}

-(void)taskControllerSetIndexNoti:(NSNotification *)noti {
    NSInteger index = [noti.object integerValue];
    self.pageViewController.selectedIndex = index;
}

- (UIViewController *)pageViewController:(XLPageViewController *)pageViewController viewControllerForIndex:(NSInteger)index {
    if (index == 0) {
        return self.searchVC;
    } else if (index == 1) {
        return self.areaVC;
    } else if (index == 2) {
        return self.gameVC;
    } else {
        return self.cardVC;
    }
}

- (NSString *)pageViewController:(XLPageViewController *)pageViewController titleForIndex:(NSInteger)index {
    return self.titles[index];
}

- (NSInteger)pageViewControllerNumberOfPage {
    return self.titles.count;
}

-(__kindof XLPageTitleCell *)pageViewController:(XLPageViewController *)pageViewController titleViewCellForItemAtIndex:(NSInteger)index {
    LCTaskPageTitleCell * cell = [pageViewController dequeueReusableTitleViewCellWithIdentifier:@"LCTaskPageTitleCell" forIndex:index];
    cell.title = self.titles[index];
    return cell;
}

- (void)pageViewController:(XLPageViewController *)pageViewController didSelectedAtIndex:(NSInteger)index {
}


@end
